
***************************************************
* Figure C.3 
***************************************************

use "$workdata/cohort", clear /* see $data_do/cohort.do */

preserve
tempfile all_ages
collapse (sum) e if age_q>=21 & age_q<29, by(pnr age_q)
g p_a=1
collapse (sum) p_a, by(pnr)
keep if p_a==96
g all_ages=1
save `all_ages'
restore
capture drop _merge
merge m:1 pnr using `all_ages'
capture drop _merge

collapse sa (sum) e, by(pnr age_q b_year) /*!*/

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

***************************************************
* Figure C.3, 1-7/8 
***************************************************

eststo clear
local model=0
foreach y of varlist sa {
local num=0
forvalues year=1975/1981 {  /*!*/
local cal_year=`year'+25 /*!*/

preserve
keep if b_year==`year'    /*!*/
keep if age_q>=21 & age_q<29
if `y'==sa   local title="Social Assistance"
local min_wage=0.49
local max_wage=0.59
local min_sa  =0.1
local max_sa  =0.20
local num=`num'+1

local spec_name="2nd order poly. spline"
local spec="d age_q0 age_q0_2      dXage_q0 dXage_q0_2"

local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
reg `y' `spec' [aw=e] , vce(cluster pnr)
eststo model`model': reg `y' `spec' [aw=e] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
predict yhat
local beta_d=round(_b[d], .001)*10000
local beta_d=floor(`beta_d')/100 

collapse `y' yhat (sem) se=`y'  [aw=e], by(age_q)
g `y'_min=`y'-se*1.96
g `y'_max=`y'+se*1.96
capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
twoway (rarea `y'_min `y'_max age_q if age_month_q<24.12, color(gs13)) ///
	   (rarea `y'_min `y'_max age_q if age_month_q>25.04, color(gs13)) ///
	   (line yhat age_q if age_q< 25   /*& `y'>`min_`y'' & `y'<`max_`y''*/  & resp==0, lcolor(gs3)) ///
   	   (line yhat age_q if age_q>= 25  /*& `y'>`min_`y'' & `y'<`max_`y''*/  & resp==0, lcolor(gs3)) ///
	   (scatter `y' age_q if age_q< 25 /*& `y'>`min_`y'' & `y'<`max_`y''*/, msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	   (scatter `y' age_q if age_q>=25 /*& `y'>`min_`y'' & `y'<`max_`y''*/, msymbol(o) mlcolor(gs3) mfcolor(gs14)) ///
	   (scatter `y' age_q if `resp', msymbol(o) mlcolor(gs3) mfcolor(gs16)) ///
	    , ylabel(`min_`y''(0.02)`max_`y'') ytitle(`title', height(5))  ///
	     xlabel(21(1)29)        xtitle("Age in months", height(5))      ///
	     xline(24.958333, lpattern(solid)) xline(/*24.875*/ 25.291667, lpattern(dash)) scheme(s1mono) legen(off) name(figC_coh_`num', replace) note("Birth Year: `year' (Age 25 in `cal_year'), Effect size: `beta_d' pp (Specification: `spec_name')" 

graph save figC_coh_`num'  $figures/figC_coh_`num', replace
graph export $figures/figC_coh_`num'.pdf, name(figC_coh_`num') replace
graph export $figures/figC_coh_`num'.png, name(figC_coh_`num') replace

restore
}
}  



***************************************************
* Figure C.3, 8/8
***************************************************

use "$workdata/cohort", clear /* see $data_do/cohort.do */

keep if b_year>=1975 & b_year<=1981
capture drop age_q
g age_q=floor( (age-floor(age))*12)*(1/12) + floor(age)
keep if age_q>=21 & age_q<29
g e=1
replace month=12 if month==0
capture drop b_year b_month
g b_year=year(foed_dag)
g b_month=month(foed_dag)

preserve
tempfile all_ages
collapse (sum) e if age_q>=21 & age_q<29, by(pnr age_q)
g p_a=1
collapse (sum) p_a, by(pnr)
keep if p_a==96
g all_ages=1
save `all_ages'
restore
capture drop _merge
merge m:1 pnr using `all_ages'
capture drop _merge

collapse sa (sum) e, by(pnr age_q)         

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************

eststo clear
local model=0
foreach y of varlist sa {
local year="all"          

preserve
keep if age_q>=21 & age_q<29
if `y'==sa   local title="Social Assistance"
local min_wage=0.49
local max_wage=0.59
local min_sa  =0.1
local max_sa  =0.20
local num=8

local spec_name="2nd order poly. spline"

local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
reg `y' `spec' [aw=e] , vce(cluster pnr)
eststo model`model': reg `y' `spec' [aw=e] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
predict yhat
local beta_d=round(_b[d], .001)*10000
local beta_d=floor(`beta_d')/100 

collapse `y' yhat (sem) se=`y'  [aw=e], by(age_q)
g `y'_min=`y'-se*1.96
g `y'_max=`y'+se*1.96
capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
twoway (rarea `y'_min `y'_max age_q if age_month_q<24.12, color(gs13)) ///
	   (rarea `y'_min `y'_max age_q if age_month_q>25.04, color(gs13)) ///
	   (line yhat age_q if age_q< 25     & resp==0, lcolor(gs3)) ///
   	   (line yhat age_q if age_q>= 25    & resp==0, lcolor(gs3)) ///
	   (scatter `y' age_q if age_q< 25  , msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	   (scatter `y' age_q if age_q>=25  , msymbol(o) mlcolor(gs3) mfcolor(gs14)) ///
	   (scatter `y' age_q if `resp', msymbol(o) mlcolor(gs3) mfcolor(gs16)) ///
	    , ylabel(`min_`y''(0.02)`max_`y'') ytitle(`title', height(5))  ///
	     xlabel(21(1)29)        xtitle("Age in months", height(5))      ///
	     xline(24.958333, lpattern(solid)) xline( 25.291667, lpattern(dash)) scheme(s1mono) legen(off) name(figC_coh_`num', replace) note( "Birth Years: `year' (Age 25 in 2000-06), Effect size: `beta_d' pp (Specification: `spec_name')")

graph save figC_coh_`num'  $figures/figC_coh_`num', replace
graph export $figures/figC_coh_`num'.pdf, name(figC_coh_`num') replace
graph export $figures/figC_coh_`num'.png, name(figC_coh_`num') replace

restore
}

graph use "$figures\figC_coh_1"
graph use "$figures\figC_coh_2"
graph use "$figures\figC_coh_3"
graph use "$figures\figC_coh_4"
graph use "$figures\figC_coh_5"
graph use "$figures\figC_coh_6"
graph use "$figures\figC_coh_7"
graph use "$figures\figC_coh_8"
graph combine figC_coh_1 figC_coh_2 figC_coh_3 figC_coh_4 figC_coh_5 figC_coh_6 figC_coh_7 figC_coh_8 ///
, cols(2) rows(4) ysize(16) xsize(11) altshrink graphregion(margin(zero)) scheme(s1mono) name(figC3, replace)
graph save figC3  $figures/figC3, replace
graph export $figures/figC3.pdf, name(figC3) replace
graph export $figures/figC3.png, name(figC3) replace

***************************************************
* end: Figure C.3 
***************************************************
